//index.js
//获取应用实例
const app = getApp();
const http = require('../../utils/http');
// const order = ['red', 'yellow', 'blue', 'green', 'red'];
const util = require('util.js')
Page({
    data: {
        total: '',
        height: '',
        searchKeyword: '',  //需要搜索的字符
        searchSongList: [], //放置返回数据的数组
        isFromSearch: true,   // 用于判断searchSongList数组是不是空数组，默认true，空的数组
        searchPageNum: 1,   // 设置加载的第几次，默认是第一次
        callbackcount: 10,      //返回数据的个数
        searchLoading: false, //"上拉加载"的变量，默认false，隐藏
        searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏

    },
    onLoad: function (options) {
        this.getTotal();
        //加载页面
        wx.getSystemInfo({
            success: (res) => {
                console.log(res.windowHeight);
                this.setData({
                    height: res.windowHeight
                })
                this.fetchSearchList();
            }
        })
    },
//总积分
    getTotal: function () {
        // http._post("/mini/getPointsTotal?openid=ociOP4mWdxJeMTXuWfY0xMbTO2Mk").then(res => {
        http._post("/mini/getPointsTotal?openid=" + app._openid).then(res => {
            if (res.code == 200) {
                this.setData({
                    'total': res.result,
                });
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none',
                    duration: 2000
                });
                setTimeout(function () {
                    wx.hideToast()
                }, 2000)
            }
        }).catch(e => {
            console.log(e)
        });
    },
    //搜索，访问网络
    fetchSearchList: function () {
        let that = this;
        // categoryId
        // let searchKeyword = 'ociOP4mWdxJeMTXuWfY0xMbTO2Mk',//openid
            let searchKeyword = app._openid,//openid
            searchPageNum = that.data.searchPageNum,//把第几次加载次数作为参数
            callbackcount = that.data.callbackcount; //返回数据的个数
        //访问网络
        util.getSearchMusic(searchKeyword, searchPageNum, callbackcount, function (data) {
            //判断是否有数据，有则取数据
            if (data.size != 0) {
                let searchList = [];
                //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加
                that.data.isFromSearch ? searchList = data.list : searchList = that.data.searchSongList.concat(data.list);
                if (data.size < callbackcount) {
                    that.setData({
                        searchSongList: searchList, //获取数据数组
                        searchLoadingComplete: true, //把“没有数据”设为true，显示
                        searchLoading: false  //把"上拉加载"的变量设为false，隐藏
                    });
                } else {
                    that.setData({
                        searchSongList: searchList, //获取数据数组
                        searchLoading: true   //把"上拉加载"的变量设为false，显示
                    });
                }

            } else {
                let searchList = [];
                //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加
                that.data.isFromSearch ? searchList = data.list : searchList = that.data.searchSongList.concat(data.list);
                that.setData({
                    searchSongList: searchList,
                    searchLoadingComplete: true, //把“没有数据”设为true，显示
                    searchLoading: false  //把"上拉加载"的变量设为false，隐藏
                });
            }
        })
    },
    //滚动到底部触发事件
    searchScrollLower: function () {
        let that = this;
        if (that.data.searchLoading && !that.data.searchLoadingComplete) {
            that.setData({
                searchPageNum: that.data.searchPageNum + 1,  //每次触发上拉事件，把searchPageNum+1
                isFromSearch: false  //触发到上拉事件，把isFromSearch设为为false
            });
            that.fetchSearchList();
        }
    },
    //分享
    onShareAppMessage: function () {
        return {
            title: '优惠券',
            desc: '优惠多多，实惠多多',
            path: '/pages/index/index'
        }
    }

})